<?php

namespace app\common\model;

use think\facade\Db;


class ShopBrandModel extends CommonModel{

    protected  $name = 'shop_brand';

    protected  $pk   = 'id';


    public function childbrand()

    {
        return $this->hasMany(ShopBrandModel::class,'parent_id','id');

    }

    public function pbrand()

    {
        return $this->hasOne(ShopBrandModel::class,'id','parent_id');

    }

    public static function info($where){
        $info = self::where(['is_delete'=>0])
            ->when(isset($where['id']),function ($query) use ($where){
                $query->where(['id'=>$where['id']]);
            })->with(['childbrand'=>function($query){
                $query->where(['is_delete'=>0]);
            }])
            ->field("*")
            ->find();
        return $info;
    }
    public static function list($where,$order,$page=1,$limit=10){
        $query = self::where(['is_delete'=>0])
            ->when(isset($where['parent_id']),function ($query) use ($where){
                $query->where(['parent_id'=>$where['parent_id']]);
            })
            ->when(isset($where['id']),function ($query) use ($where){
                $query->where(['id'=>$where['id']]);
            })->with(['childbrand'=>function($query){
                $query->where(['is_delete'=>0]);
            }])
            ->field("*");
        $count=$query->count();
        $list=$query->order($order)
            ->limit($limit)->page($page)
            ->select()
            ->toArray();
        return compact('count','list');
    }
    public static function up($data,$where){
        $data['edit_time']=date('Y-m-d H:i:s');
        $res=self::update($data,$where);
        return $res;
    }
}

